package Main;

import javax.swing.JApplet;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import Main.PayDayMain;
import EventManager.EventManager;
import Gui.GameFrame;

public class GameApplet extends JApplet implements ActionListener
{
	/**
	 * <APPLET ARCHIVE="game.jar" CODE="gsp420.game.GameApplet.class" WIDTH=90%
	 * HEIGHT=90%></APPLET>
	 */
	private JMenuBar gameMenuBar;
	private JMenuItem gameNewMenuItem;
	private JMenuItem gameAboutItem;
	private JMenuItem gameInstructionsItem;
	private JMenuItem gameExitMenuItem;
	private JMenuItem gameOptionsItem;
	
	private static final long serialVersionUID = 4589616916588049174L;

	public GameApplet()
	{
	}

	public void init()
	{
		setLayout(new BorderLayout());
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		int height = screenSize.height;
		int width = screenSize.width;
		setSize((width / 2)+50, (height / 2));
		buildMenu();
		
		setContentPane(GameFrame.getInstance());
	//	add(GameFrame.getInstance(), BorderLayout.CENTER);
		Thread t = new Thread(new PayDayMain());
		t.start();
		
	}

	public void start()
	{
	}

	public void stop()
	{
	}

	public void destroy()
	{
	}
	/**
	 * Creates the Menubar
	 */
	private void buildMenu()
	{
		gameMenuBar = new JMenuBar();
		setJMenuBar(gameMenuBar);
		JMenu jmenu = new JMenu("Game");
		jmenu.setMnemonic(KeyEvent.VK_G);
		
		gameNewMenuItem = new JMenuItem("New Game", KeyEvent.VK_N);
		
		gameNewMenuItem.addActionListener(this);
		jmenu.add(gameNewMenuItem);
		gameOptionsItem = new JMenuItem("Options", KeyEvent.VK_P);
		
		gameOptionsItem.addActionListener(this);
		jmenu.add(gameOptionsItem);
		gameExitMenuItem = new JMenuItem("Exit", KeyEvent.VK_X);
		gameExitMenuItem.addActionListener(this);
		jmenu.add(gameExitMenuItem);
		gameMenuBar.add(jmenu);
		JMenu jmenu2 = new JMenu("Help");
		jmenu2.setMnemonic(KeyEvent.VK_H);
		gameInstructionsItem = new JMenuItem("Instructions",KeyEvent.VK_I);
		gameInstructionsItem.addActionListener(this);
		jmenu2.add(gameInstructionsItem);
		gameAboutItem = new JMenuItem("About",KeyEvent.VK_A);
		gameAboutItem.addActionListener(this);
		jmenu2.add(gameAboutItem);
		
		gameMenuBar.add(jmenu2);
	}

	@Override
	public void actionPerformed(ActionEvent actionevent)
	{
		/**
		 * Exits the game
		 */
		if (actionevent.getSource() == gameExitMenuItem)
		{
			System.exit(0); // REPLACE WITH m_running = false
		}
		else if (actionevent.getSource() == gameNewMenuItem)
		{
			EventManager.getInstance().queueEvent("EVENT_START_GAME");
		}
		else if (actionevent.getSource() == gameOptionsItem)
		{
			EventManager.getInstance().queueEvent("EVENT_OPTIONS_MENU");
		}
		else if (actionevent.getSource() == gameInstructionsItem)
		{
			EventManager.getInstance().queueEvent("EVENT_INSTRUCTIONS_MENU");
		}
		else if (actionevent.getSource() == gameAboutItem)
		{
			EventManager.getInstance().queueEvent("EVENT_ABOUT_LOAD");
		}
		
	}
}
